library(ggpubr)
library(ggrepel)
library(clusterProfiler)
library(tidyverse)
source('00_util_scripts/mod_seurat.R')
source('00_util_scripts/mod_bplot.R')

# volcano --------
sdeg.subsets.g2r <- read_csv('mission/ye_AH-cOME/deg.subsets.g2r.csv')

sdeg.subsets.g2r |>
  filter(gene == 'TNFRSF11B')

volc.g2r <- sdeg.subsets.g2r |>
  filter(!str_detect(gene, '^ENSG')) |>
  nest(data = -subtype) |>
  mutate(volc = map2(data, subtype,
                     \(x,y)plot_bill_volc(x, 'RR') + ggtitle(y))) |>
  pull(volc)

volc.g2r

# enrichment plot ------
sdeg.subsets <- read_csv('mission/ye_AH-cOME/deg.subsets.csv')

upgo.plasma <- sdeg.subsets |>
  filter(subtype == 'classical.MBC') |>
  filter(avg_log2FC > 0) |>
  pull(gene) |>
  enrichGO(OrgDb = 'org.Hs.eg.db',
           keyType = 'SYMBOL',
           ont = 'BP',
           readable = T) |>
  simplify() 

upgo.plasma |>
  pluck('result') |>
  plot_enrichment()

downgo.plasma <- sdeg.nrp |>
  filter(subtype == 'classical.MBC') |>
  filter(avg_log2FC < 0) |>
  pull(gene) |>
  enrichGO(OrgDb = 'org.Hs.eg.db',
           keyType = 'SYMBOL',
           ont = 'BP',
           readable = T) |>
  simplify() 

downgo.plasma |>
  pluck('result') |>
  plot_enrichment(base_col = 'blue')

## RR vs GG -----
sdeg.subsets.rrgg <- read_csv('mission/ye_AH-cOME/deg.subsets.rrgg.csv')

sdeg.subsets.rrgg |>
  filter(gene == 'TNFRSF11B')

upgo.rrgg <- come.type.list |>
  map(\(x)sdeg.subsets.rrgg |>
  filter(subtype == x) |>
  filter(avg_log2FC > 0) |>
  pull(gene) |>
  enrichGO(OrgDb = 'org.Hs.eg.db',
           keyType = 'SYMBOL',
           ont = 'BP',
           readable = T) |>
  simplify() |>
  pluck('result'), .progress = T) |>
  list_rbind(names_to = 'celltype') |>
  as_tibble()

upkegg.rrgg <- come.type.list |>
  map(\(x)sdeg.subsets.rrgg |>
        filter(subtype == x) |>
        filter(avg_log2FC > 0) |>
        pull(gene) |>
        bitr(fromType = 'SYMBOL', toType = 'ENTREZID',
             OrgDb = 'org.Hs.eg.db') |>
        pull(ENTREZID) |>
        enrichKEGG() |>
        pluck('result'), .progress = T) |>
  list_rbind(names_to = 'celltype') |>
  as_tibble()

upgo.rrgg |>
  filter(celltype == 'plasmablast') |>
  plot_enrichment() +
  labs(title = 'RR genotype plasmablast upregulation: GO enrichment')

upkegg.rrgg |>
  filter(celltype == 'FCRL4.MBC') |>
  plot_enrichment() +
  labs(title = 'FCRL4+ MBC: KEGG enrichment')

downgo.rrgg <- come.type.list |>
  map(\(x)sdeg.subsets.rrgg |>
        filter(subtype == x) |>
        filter(avg_log2FC < 0) |>
        pull(gene) |>
        enrichGO(OrgDb = 'org.Hs.eg.db',
                 keyType = 'SYMBOL',
                 ont = 'BP',
                 readable = T) |>
        simplify() |>
        pluck('result'), .progress = T) |>
  list_rbind(names_to = 'celltype') |>
  as_tibble()

downgo.rrgg |>
  filter(celltype == 'plasmablast') |>
  plot_enrichment(base_col = 'blue') +
  labs(title = 'RR genotype plasmablast downregulation: GO enrichment')

## allele exlcuded R vs G --------
sdeg.al.ex.g2r

upgo <- sdeg.al.ex.g2r |>
  filter(p_val < .05, avg_log2FC > 0) |>
  pull(gene) |>
  enrichGO(OrgDb = 'org.Hs.eg.db',
           keyType = 'SYMBOL',
           ont = 'ALL',
           minGSSize = 3, readable = T)


# IGG subtypes in sdeg ---------
## AH+cOME vs AH
sdeg.subsets |>
  filter(str_detect(gene, '^IGH[D|M|A|E|G]')) |>
  ggplot(aes(gene, subtype, color = avg_log2FC, size = -log10(p_val_adj))) +
  geom_point() +
  scale_color_gradient2(low = 'blue', high = 'red') +
  theme_pubr(legend = 'right') +
  labs(title = 'Significant IGH expression changes in AH+cOME vs AH B cells')

## RR vs GR+GG
sdeg.subsets.g2r |>
  filter(str_detect(gene, '^IGH[D|M|A|E|G]')) |>
  ggplot(aes(gene, subtype, color = avg_log2FC, size = -log10(p_val_adj))) +
  geom_point() +
  scale_color_gradient2(low = 'blue', high = 'red') +
  theme_pubr(legend = 'right') +
  labs(title = 'Significant IGH expression changes in IGHG1-RR vs GG+GR B cells')

## GG vs GR+RR
sdeg.subsets.gg |>
  mutate(avg_log2FC = -avg_log2FC) |>
  filter(str_detect(gene, '^IGH[D|M|A|E|G]')) |>
  ggplot(aes(gene, subtype, color = avg_log2FC, size = -log10(p_val_adj))) +
  geom_point() +
  scale_color_gradient2(low = 'blue', high = 'red') +
  theme_pubr(legend = 'right') +
  labs(title = 'Significant IGH expression changes in IGHG1-GR+RR vs GG B cells')

## RR vs GG
sdeg.subsets.rrgg |>
  filter(str_detect(gene, '^IGH[D|M|A|E|G]')) |>
  ggplot(aes(gene, subtype, color = avg_log2FC, size = -log10(p_val_adj))) +
  geom_point() +
  scale_color_gradient2(low = 'blue', high = 'red') +
  theme_pubr(legend = 'right') +
  labs(title = 'Significant IGH expression changes in IGHG1-RR vs GG B cells')

## IT vs II
sdeg.subsets.i2t |>
  filter(str_detect(gene, '^IGH[D|M|A|E|G]')) |>
  ggplot(aes(gene, subtype, color = avg_log2FC, size = -log10(p_val_adj))) +
  geom_point() +
  scale_color_gradient2(low = 'blue', high = 'red') +
  theme_pubr(legend = 'right') +
  labs(title = 'Significant IGH expression changes in FCGR2B-IT vs II B cells')
